﻿using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using AEM.Utils;

namespace AEM.Data.DAL
{
    public static class TypeLib
    {
        public static bool Add(Model.TypeLib value)
        {
            string sql = @"INSERT INTO TableTypeLib
                (TypeLibNum, LiftType, FloorFormat, StatusPoints)
                VALUES   (@TypeLibNum,@LiftType,@FloorFormat,@StatusPoints)";

            int ret = AccessHelper.ExecuteNonQuery(sql, Param(value));
            if (-1 != ret) return true;
            return false;
        }

        public static bool Update(Model.TypeLib value)
        {
            string sql = @"UPDATE  TableTypeLib SET 
                        TypeLibNum=@TypeLibNum, LiftType=@LiftType,FloorFormat=@FloorFormat, StatusPoints=@StatusPoints
                        WHERE [ID]=@ID";

            int ret = AccessHelper.ExecuteNonQuery(sql, Param(value));
            if (-1 != ret) return true;
            return false;
        }

        private static OleDbParameter[] Param(Model.TypeLib args)
        {
            List<OleDbParameter> param = new List<OleDbParameter>();
            param.Add(new OleDbParameter("@TypeLibNum", args.TypeLibNum));
            param.Add(new OleDbParameter("@LiftType", args.LiftType));
            param.Add(new OleDbParameter("@FloorFormat", args.FloorFormat));
            param.Add(new OleDbParameter("@StatusPoints", args.StatusPoints));
            param.Add(new OleDbParameter("@ID", args.ID));
            return param.ToArray();
        }

        public static Model.TypeLib Select(int id)
        {
            return Select("ID", id.ToString());
        }

        public static Model.TypeLib Select(string field, string value)
        {
            string condition = " " + field + "={0}";
            if (field == "ID")
                condition = string.Format(condition, value);
            else
                condition = string.Format(condition, "'" + value + "'");
            DataTable dt = Global.List("TableTypeLib", condition);
            if (null != dt && dt.Rows.Count > 0)
            {
                return Deserialize(dt.Rows[0]);
            }
            return null;
        }

        private static Model.TypeLib Deserialize(DataRow dr)
        {
            Model.TypeLib model = null;
            if (null != dr)
            {
                model = new Model.TypeLib();
                model.ID = dr["ID"].ToString().toInt32();
                model.TypeLibNum = dr["TypeLibNum"].ToString();
                model.LiftType = dr["LiftType"].ToString().toInt32();
                model.FloorFormat = dr["FloorFormat"].ToString().toInt32();
                model.StatusPoints = dr["StatusPoints"].ToString();
            }
            return model;
        }

        public static List<Model.TypeLib> List(string condition)
        {
            DataTable dt = Global.List("TableTypeLib", condition);
            if (null != dt && dt.Rows.Count > 0)
            {
                return Deserialize(dt);
            }
            return null;
        }

        private static List<Model.TypeLib> Deserialize(DataTable dt)
        {
            List<Model.TypeLib> list = null;
            if (null != dt && dt.Rows.Count > 0)
            {
                list = new List<Model.TypeLib>();
                foreach (DataRow r in dt.Rows)
                {
                    list.Add(Deserialize(r));
                }
            }
            return list;
        }


        internal static bool Delete(int id)
        {
            return Global.Delete("TableTypeLib", "ID", id.ToString());
        }
    }
}
